$(function () {
    obj.init();
});
var obj = new function () {
    'use strict';
    return {
        form:null,
        init:function () {
            this.form = new xhForm($('#xh_form'));
            this.form.parse();
            this.form.bindRequired($('#bt_modal_confirm',parent.window.document));
            $('[name="sStudents[]"],[name="iBBPId"]').select2();
            this.listener();
        },
        listener:function () {
            var $this =this;

            //流程计划选中
            $('[name=iBBPId]').on('change',function () {
                $this.funObj().bindSelectPlan($(this).val());
            });
            //学生选中
            $('[name="sStudents[]"]').on('change',function () {
                $this.funObj().triggerNumChange($(this).val());
            });
            //确认
            $('#bt_modal_confirm',parent.window.document).click(function () {
                $this.funObj().submitForm();
            });



        },
        funObj:function () {
            var $this = this;
            return {
                bindSelectPlan:function (planId) {
                    xhExtra.ajaxRequest('device/ajaxStuFromPlan' , {planId:planId} , {} , function (res) {
                        var dom = $('[name="sStudents[]"]');
                        dom.empty();
                        var str = '';
                        $.each(res,function (key,value) {
                            str += '<option value="'+key+'">'+value+'</option>';
                        });
                        dom.append(str);
                    })
                },
                triggerNumChange:function (arr) {
                    $('[name=iStuNum]').val(arr.length);
                },
                submitForm:function () {
                    var iShellNum = $('[name=iShellNum]').val();
                    var iShellId = $('[name=iShellId]').val();
                    if ( (iShellNum && !iShellId) || (iShellId && !iShellNum)) {
                        window.top.xhTop.showError('平板壳选择不正确');
                        xhExtra.setBtnNormal( $('#bt_modal_confirm',parent.window.document));
                        return false;
                    }
                    var params = {};
                    if (iBBPId) {
                        params.iBBPId = iBBPId;
                    }
                    $this.form.submit('device/saveStuApply',params,function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                        try {
                            window.top.xhTop.getActiveFrame().obj.dataGrid.reload();
                            window.top.xhTop.closeModal();
                        } catch ($e) {
                            window.top.xhTop.getActiveFrame().planList.treegrid('reload');
                            window.top.xhTop.closeModal();
                        }


                    })
                }
            }
        }
    }
};